unit mainform;

{$mode delphi}

interface

uses
  // RTL, FCL, LCL
  Classes, SysUtils, db, SdfData, FileUtil, LResources, Forms, Controls,
  Graphics, Dialogs, ExtCtrls, Buttons, ActnList, StdCtrls, Spin, DBGrids,
  MaskEdit, LCLType, MD5, LCLProc,
  // TesteDeVisao
  drawer, testlogic, constants, config, dbmanager;

type

  { TTestMainForm }

  TTestMainForm = class(TForm)
    actNewTest: TAction;
    actionsMainForm: TActionList;
    botaoLogarComoAdmin: TBitBtn;
    botaoLogarComoUsuario: TBitBtn;
    botaoMudarSenha: TBitBtn;
    buttonLogOut: TBitBtn;
    buttonConfigurations: TBitBtn;
    buttonHistorico: TBitBtn;
    buttonStartTest: TBitBtn;
    buttonNewTest: TBitBtn;
    comboUsuario: TComboBox;
    Datasource: TDatasource;
    DBGrid: TDBGrid;
    groupConfigAdmin: TGroupBox;
    labelSenha: TLabel;
    textSenhaLogin: TEdit;
    group_Welcome_Usuario: TGroupBox;
    group_Welcome_admin: TGroupBox;
    label_Config_BarSize: TLabel;
    label_Welcome_UserName: TLabel;
    label_Welcome_Password: TLabel;
    label_Config_ScreenDistance: TLabel;
    label_Instructions_Instructions: TLabel;
    label_Instructions_Title: TLabel;
    label_Historico_List_Caption: TLabel;
    label_Historico_Title: TLabel;
    label_Historico_Instructions: TLabel;
    label_Welcome_Title: TLabel;
    label_Welcome_Instructions: TLabel;
    labelInstructions: TLabel;
    labelArcmins: TLabel;
    notebookWorkspace: TNotebook;
    pageWelcome: TPage;
    pageHistorico: TPage;
    pageTestInstructions: TPage;
    pageTest: TPage;
    pageConfigurations: TPage;
    panelInstructions: TPanel;
    panelTop: TPanel;
    shapeBar: TShape;
    spinDistance: TSpinEdit;
    spinBar: TSpinEdit;
    textNovaSenha: TEdit;
    procedure actNewTestExecute(Sender: TObject);
    procedure botaoLogarComoAdminClick(Sender: TObject);
    procedure botaoLogarComoUsuarioClick(Sender: TObject);
    procedure botaoMudarSenhaClick(Sender: TObject);
    procedure buttonConfigurationsClick(Sender: TObject);
    procedure buttonLogOutClick(Sender: TObject);
    procedure buttonStartTestClick(Sender: TObject);
    procedure buttonHistoricoClick(Sender: TObject);
    procedure DoUpdateResolution(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    TestDrawer: TTestDrawer;
    procedure DoUpdateUIFromLogic(Sender: TObject);
  end; 

var
  TestMainForm: TTestMainForm;

implementation

{ TTestMainForm }

procedure TTestMainForm.FormCreate(Sender: TObject);
begin
  TestDrawer := TTestDrawer.Create(Self);
  TestDrawer.Parent := pageTest;
  TestDrawer.Align := alClient;
  TestDrawer.OnKeyPress := gTestLogic.HandleKeyPress;
  TestDrawer.OnKeyDown := gTestLogic.HandleKeyDown;
  gTestLogic.OnUpdateUI := DoUpdateUIFromLogic;
  gTestLogic.Drawer := TestDrawer;

  Self.Height := INT_WINDOW_HEIGHT;
  Self.Width := INT_WINDOW_WIDTH;

  comboUsuario.Items.Assign(gTestConfig.Usuarios);

  notebookWorkspace.ActivePageComponent := pageWelcome;

  { Auto-calculate screend resolution upon program start }
  shapeBar.Width := gTestConfig.BarSizeInPixels;
  spinBar.Value := Round(shapeBar.Width / gTestConfig.PixelsPerMilimeter);
  DoUpdateResolution(Self);

  { Activate database }
  Datasource.Dataset := gDBManager.Dataset;
end;

procedure TTestMainForm.DoUpdateUIFromLogic(Sender: TObject);
begin
  labelInstructions.Caption := gTestLogic.Instructions;
end;

procedure TTestMainForm.actNewTestExecute(Sender: TObject);
begin
  notebookWorkspace.ActivePageComponent := pageTestInstructions;
end;

procedure TTestMainForm.botaoLogarComoAdminClick(Sender: TObject);
begin
  // Verifica a senha
  if ((textSenhaLogin.Text = '') and (gTestConfig.HashDaSenha = ''))
   or (MDPrint(MD5String(textSenhaLogin.Text)) = gTestConfig.HashDaSenha) then
  begin
    // Clean password field so that it doesn't remain for the future
    textSenhaLogin.Text := '';

    // Seleciona o usuario atual
    gTestConfig.UsuarioAtual := 'admin';

    // Prepara a interface
    buttonConfigurations.Enabled := True;
    buttonHistorico.Enabled := True;
    buttonLogOut.Enabled := True;
    groupConfigAdmin.Visible := True;

    notebookWorkspace.ActivePageComponent := pageHistorico;
  end
  else
    Application.MessageBox('Senha errada.', Str_Brtablet, MB_OK);
end;

procedure TTestMainForm.botaoLogarComoUsuarioClick(Sender: TObject);
var
  Index: Integer;
begin
  // Valida a entrada do usuario
  if comboUsuario.Text = '' then
  begin
    Application.MessageBox('O nome do usuário escolhido não pode ser vazio', Str_Brtablet, MB_OK);
    Exit;
  end;

  if comboUsuario.Text = 'admin' then
  begin
    Application.MessageBox('O nome do usuário escolhido não pode ser "admin"', Str_Brtablet, MB_OK);
    Exit;
  end;

  // Se for novo usuario, adiciona-lo
  Index := 0;
  if not gTestConfig.Usuarios.Find(comboUsuario.Text, Index) then
    gTestConfig.Usuarios.Add(comboUsuario.Text);

  // Seleciona o usuario atual
  gTestConfig.UsuarioAtual := comboUsuario.Text;

  // Prepara a interface
  buttonNewTest.Enabled := True;
  buttonConfigurations.Enabled := True;
  buttonLogOut.Enabled := True;
  groupConfigAdmin.Visible := False;

  notebookWorkspace.ActivePageComponent := pageConfigurations;
end;

procedure TTestMainForm.botaoMudarSenhaClick(Sender: TObject);
begin
  // Define a nova senha
  if textNovaSenha.Text = '' then gTestConfig.HashDaSenha := ''
  else gTestConfig.HashDaSenha := MDPrint(MD5String(textNovaSenha.Text));

  Application.MessageBox('Senha alterada com sucesso!', Str_Brtablet, MB_OK);
end;

procedure TTestMainForm.buttonConfigurationsClick(Sender: TObject);
begin
  notebookWorkspace.ActivePageComponent := pageConfigurations;
  buttonHistorico.Enabled := False;
end;

procedure TTestMainForm.buttonLogOutClick(Sender: TObject);
begin
  // Prepara a interface
  buttonNewTest.Enabled := False;
  buttonConfigurations.Enabled := False;
  buttonHistorico.Enabled := False;
  buttonLogOut.Enabled := False;

  notebookWorkspace.ActivePageComponent := pageWelcome;
end;

procedure TTestMainForm.buttonStartTestClick(Sender: TObject);
begin
  notebookWorkspace.ActivePageComponent := pageTest;
  Self.ActiveControl := TestDrawer;
  TestDrawer.SetFocus;
  gTestLogic.NewTest;
end;

procedure TTestMainForm.buttonHistoricoClick(Sender: TObject);
begin
  notebookWorkspace.ActivePageComponent := pageHistorico;
end;

procedure TTestMainForm.DoUpdateResolution(Sender: TObject);
begin
  gTestConfig.ScreenDistance := spinDistance.Value;
  gTestConfig.BarSize := spinBar.Value;
  labelArcmins.Caption := Format(Str_Arcmins,
   [gTestConfig.ArcminsPerPixel, gTestConfig.MaxVA]);
end;

procedure TTestMainForm.FormActivate(Sender: TObject);
begin
  if gTestLogic.IsTestRunning then
    TestDrawer.SetFocus;
end;

initialization
  {$I mainform.lrs}
end.

